﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EnnTrading.DataAccess.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace MyWCF.Demo.Client.Web.Code
{
    public class ChatRoomMessagesDB
    {
        private const string PARAM_MESSAGE_ID = "@MessageId";
        private const string PARAM_USER_ID = "@UserId";
        private const string PARAM_CHAT_ROOM_ID = "@ChatRoomId";
        private const string PARAM_MESSAGE = "@Message";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_START = "@Start";
        private const string PARAM_COUNT = "@Count";

        private static string ConnectionString = SQLHelper.CONN_STRING;

        private static void FillEntityFrom(SqlDataReader reader, ChatRoomMessagesEntity obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.MessageId = reader.IsDBNull(reader.GetOrdinal("MessageId")) ? 0 : reader.GetInt64(reader.GetOrdinal("MessageId"));
                obj.UserId = reader.IsDBNull(reader.GetOrdinal("UserId")) ? 0 : reader.GetInt64(reader.GetOrdinal("UserId"));
                obj.ChatRoomId = reader.IsDBNull(reader.GetOrdinal("ChatRoomId")) ? 0 : reader.GetInt64(reader.GetOrdinal("ChatRoomId"));
                obj.Message = reader.IsDBNull(reader.GetOrdinal("Message")) ? String.Empty : reader.GetString(reader.GetOrdinal("Message"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.Now : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
            }
            #endregion
        }

        public static bool Add(ChatRoomMessagesEntity obj)
        {
            SqlParameter[] dbParams ={
							 SQLHelper.MakeParam(PARAM_USER_ID, SqlDbType.BigInt,0,obj.UserId),
							 SQLHelper.MakeParam(PARAM_CHAT_ROOM_ID, SqlDbType.BigInt,0,obj.ChatRoomId),
							 SQLHelper.MakeParam(PARAM_MESSAGE, SqlDbType.NVarChar, 200,obj.Message)					
					};

            var result = SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ChatRoomMessages_Add", dbParams);
            if (result != DBNull.Value)
            {
                obj.MessageId = long.Parse(result.ToString());
                return true;
            }
            return false;
        }

        public static List<ChatRoomMessagesEntity> GetListByChatRoomId(long chatRoomId, long lastMessageId)
        {
            SqlDataReader reader = null;
            List<ChatRoomMessagesEntity> result = new List<ChatRoomMessagesEntity>();
            try
            {
                SqlParameter[] dbParams ={
							 SQLHelper.MakeParam(PARAM_CHAT_ROOM_ID, SqlDbType.BigInt,0,chatRoomId)	,
			                 SQLHelper.MakeParam("@LastMessageId", SqlDbType.BigInt,0,lastMessageId)
					};
                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ChatRoomMessages_GetListByChatRoomId", dbParams);
                while (reader.Read())
                {
                    ChatRoomMessagesEntity entity = new ChatRoomMessagesEntity();
                    FillEntityFrom(reader, entity);

                    result.Add(entity);
                }
            }
            finally
            {
                if (reader != null && reader.IsClosed == false)
                {
                    reader.Close();
                }
            }
            return result;
        }

        public static List<ChatRoomMessagesEntity> GetChatRoomMessageList(long userId, long lastMessageId)
        {
            SqlDataReader reader = null;
            List<ChatRoomMessagesEntity> result = new List<ChatRoomMessagesEntity>();
            try
            {
                SqlParameter[] dbParams ={
							 SQLHelper.MakeParam(PARAM_USER_ID, SqlDbType.BigInt,0,userId),
                             SQLHelper.MakeParam("@LastMessageId", SqlDbType.BigInt,0,lastMessageId)
					};
                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ChatRoomMessages_GetRoomAllListByUser", dbParams);
                while (reader.Read())
                {
                    ChatRoomMessagesEntity entity = new ChatRoomMessagesEntity();
                    FillEntityFrom(reader, entity);

                    result.Add(entity);
                }
            }
            finally
            {
                if (reader != null && reader.IsClosed == false)
                {
                    reader.Close();
                }
            }
            return result;
        }
    }
}
